namespace SimpleWebServices.Models
{
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Security.Cryptography;
    using Xunit;

    public sealed class CountryFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Country>()
                .DerivesFrom<Geography>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("geography")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Country actual = (
                "<geography title='United Kingdom'>" +
                    "<data>" +
                        "<value name='IDD'>+44</value>" +
                        "<value name='ISO 3166-1 alpha-2'>GB</value>" +
                        "<value name='ISO 3166-1 alpha-3'>GBR</value>" +
                        "<value name='ISO 3166-1 numeric'>826</value>" +
                    "</data>" +
                "</geography>").Deserialize<Country>();

            Assert.NotNull(actual);

            Assert.Equal<string>("GB", actual.Alpha2);
            Assert.Equal<string>("GBR", actual.Alpha3);
            Assert.Equal<string>("+44", actual.InternationalDirectDialling);
            Assert.Equal<string>("826", actual.Numeric);
            Assert.Equal<string>("United Kingdom", actual.Title);
        }

        [Fact]
        public void deserializeEmpty()
        {
            Country actual = "<geography />".Deserialize<Country>();

            Assert.NotNull(actual);

            Assert.Null(actual.Alpha2);
            Assert.Null(actual.Alpha3);
            Assert.Null(actual.InternationalDirectDialling);
            Assert.Null(actual.Numeric);
            Assert.Null(actual.Title);
        }

        [Fact]
        public void serialize()
        {
            Country obj = new Country
            {
                InternationalDirectDialling = "+44",
                Alpha2 = "GB",
                Alpha3 = "GBR",
                Numeric = "826",
                Title = "United Kingdom"
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/geography[@title='United Kingdom']/data/value[@name='IDD'][text()='+44'])"));
            Assert.True((bool)navigator.Evaluate("1=count(/geography[@title='United Kingdom']/data/value[@name='ISO 3166-1 alpha-2'][text()='GB'])"));
            Assert.True((bool)navigator.Evaluate("1=count(/geography[@title='United Kingdom']/data/value[@name='ISO 3166-1 alpha-3'][text()='GBR'])"));
            Assert.True((bool)navigator.Evaluate("1=count(/geography[@title='United Kingdom']/data/value[@name='ISO 3166-1 numeric'][text()='826'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Country());
        }

        [Fact]
        public void prop_Alpha2()
        {
            Assert.True(new PropertyExpectations(typeof(Country).GetProperty("Alpha2"))
                .XmlIgnore()
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Alpha3()
        {
            Assert.True(new PropertyExpectations(typeof(Country).GetProperty("Alpha3"))
                .XmlIgnore()
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Entity()
        {
            Assert.True(new PropertyExpectations(typeof(Country).GetProperty("Entity"))
                .XmlIgnore()
                .TypeIs<string>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_Entity_get()
        {
            Country obj = new Country
            {
                InternationalDirectDialling = "+44",
                Alpha2 = "GB",
                Alpha3 = "GBR",
                Numeric = "826",
                Title = "United Kingdom"
            };

            string expected = "9Ah4LB4HpscGghm1DENLEA==";
            string actual = MD5Hash.Compute(obj.Entity);

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void prop_Entity_getEmpty()
        {
            string expected = string.Empty;
            string actual = new Country().Entity;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void prop_InternationalDirectDialling()
        {
            Assert.True(new PropertyExpectations(typeof(Country).GetProperty("InternationalDirectDialling"))
                .XmlIgnore()
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Numeric()
        {
            Assert.True(new PropertyExpectations(typeof(Country).GetProperty("Numeric"))
                .XmlIgnore()
                .IsAutoProperty<string>()
                .Result);
        }
    }
}